home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / xlib06p1.zip / XPBMCLIP.CPP < prev    next >
C/C++ Source or Header  |  1995-03-15  |  5KB  |  183 lines

  1. #include "xinternl.h"
  2. #include <conio.h>
  3. #include <mem.h>
  4. /*==================================================================
  5. XPBMCLIP.CPP contains the basic functions for clipped bitmap puts.
  6.  
  7. Note that the horizontal clipping coordinates are given in nibble
  8. widths, ie they clip roughly to boundaries which are multiples of
  9. four pixels.
  10.  
  11. These routines were written initially by Themie Gouthas and
  12. company and modified March 1995 by Victor B. Putz.
  13. ===================================================================*/
  14.  
  15. extern xScreenCoord_t TopClip;                /* Clipping rectangle                  */
  16. extern xScreenCoord_t BottomClip;
  17. extern xScreenCoord_t LeftClip;
  18. extern xScreenCoord_t RightClip;
  19.  
  20. extern BYTE * pbVGABuffer;
  21. extern int ScrnLogicalByteWidth;
  22.  
  23.  
  24. /*
  25. _ClipX() clips the x coordinates of a bitmap
  26. */
  27. inline void _ClipX(
  28.     xScreenCoord_t X,
  29.   int iWidth,
  30.   int * piOffsetFromLeft,
  31.   int * piWidthToDraw
  32. )
  33. {
  34.   *piOffsetFromLeft = LeftClip - ( X / 4 );
  35.   if ( *piOffsetFromLeft < 0 ) {
  36.     *piOffsetFromLeft = 0;
  37.   }
  38.   int iOffsetFromRight  = ( X / 4 + iWidth ) - RightClip;
  39.   if ( iOffsetFromRight < 0 ) {
  40.     iOffsetFromRight = 0;
  41.   }
  42.   *piWidthToDraw = iWidth - ( *piOffsetFromLeft + iOffsetFromRight );
  43. }
  44.  
  45.  
  46. /*
  47. _ClipY() clips the Y coordinates of a bitmap
  48. */
  49. inline void _ClipY(
  50.     xScreenCoord_t Y,
  51.   int iHeight,
  52.   int * piOffsetFromTop,
  53.   int * piHeightToDraw
  54. )
  55. {
  56.   *piOffsetFromTop = TopClip - Y;
  57.   if ( *piOffsetFromTop < 0 ) {
  58.     *piOffsetFromTop = 0;
  59.   }
  60.   int iOffsetFromBottom  = Y + iHeight - BottomClip;
  61.   if ( iOffsetFromBottom < 0 ) {
  62.     iOffsetFromBottom = 0;
  63.   }
  64.   *piHeightToDraw = iHeight - ( *piOffsetFromTop + iOffsetFromBottom );
  65. }
  66.  
  67.  
  68. int x_put_masked_pbm_clipxy( /* Copy a planar bitmap from SRAM masking */
  69.   xScreenCoord_t X,          /* only non zero pixels to VRAM           */
  70.   xScreenCoord_t Y,          /* Supports clipping in the Y direction   */
  71.   xPageHandle_t ScrnOffs,
  72.   BYTE * Bitmap
  73. )
  74. {
  75.   BYTE * pbSource = Bitmap;
  76.   int iWidth = *pbSource++;
  77.   int iHeight = *pbSource++;
  78.     //first let's get our clipping dimensions.  We'll need an "offset from
  79.   //left" value, a width to draw value, an "offset from top" value, and a
  80.   //"height to draw" value.
  81.     int iOffsetFromLeft;
  82.   int iWidthToDraw;
  83.   _ClipX( X, iWidth, &iOffsetFromLeft, &iWidthToDraw );
  84.   if ( iWidthToDraw < 0 ) {
  85.     return 1;
  86.   }
  87.   //now do Y
  88.   int iOffsetFromTop;
  89.   int iHeightToDraw;
  90.   _ClipY( Y, iHeight, &iOffsetFromTop, &iHeightToDraw );
  91.   if ( iHeightToDraw < 0 ) {
  92.     return 1;
  93.   }
  94.  
  95.   BYTE * pbDest = pbVGABuffer + ScrnOffs +
  96.         ScrnLogicalByteWidth * ( Y + iOffsetFromTop ) +
  97.         X / 4 + iOffsetFromLeft;
  98.   BYTE * pbDestCounter = pbDest;
  99.  
  100.   int iSkip = ScrnLogicalByteWidth - iWidthToDraw;
  101.   outp( SC_INDEX, MAP_MASK );
  102.   int iPlane = X & 3;
  103.   int iAdjust = 0;
  104.   for( int iPlaneCounter = 4; iPlaneCounter != 0; --iPlaneCounter ) {
  105.     pbDestCounter = pbDest + iAdjust;
  106.     pbSource = Bitmap + 2 + ( 4 - iPlaneCounter ) * iWidth * iHeight + iOffsetFromTop * iWidth + iOffsetFromLeft;
  107.       outp( SC_INDEX + 1, 1 << iPlane );
  108.     for ( int iRowCounter = iHeightToDraw; iRowCounter != 0; --iRowCounter ) {
  109.         for ( int iWidthCounter = iWidthToDraw; iWidthCounter != 0; --iWidthCounter ) {
  110.           BYTE bTemp = *pbSource++;
  111.           if ( bTemp ) {
  112.             *pbDestCounter++ = bTemp;
  113.           }
  114.           else {
  115.             pbDestCounter++;
  116.           }
  117.       }
  118.       pbSource += iWidth - iWidthToDraw;
  119.       pbDestCounter += iSkip;
  120.     }
  121.     iPlane++;
  122.     if ( iPlane == 4 ) {
  123.       iAdjust = 1;
  124.     }
  125.     iPlane &= 3;
  126.   }
  127.   return 0;
  128. }
  129.  
  130.  
  131. int x_put_pbm_clipxy(   /* Copy a planar bitmap from SRAM masking */
  132.   xScreenCoord_t X,     /* Supports clipping in the X&Y directions */
  133.   xScreenCoord_t Y,
  134.   xPageHandle_t ScrnOffs,
  135.   BYTE * Bitmap
  136. )
  137. {
  138.   BYTE * pbSource = Bitmap;
  139.   int iWidth = *pbSource++;
  140.   int iHeight = *pbSource++;
  141.     //first let's get our clipping dimensions.  We'll need an "offset from
  142.   //left" value, a width to draw value, an "offset from top" value, and a
  143.   //"height to draw" value.
  144.     int iOffsetFromLeft;
  145.   int iWidthToDraw;
  146.   _ClipX( X, iWidth, &iOffsetFromLeft, &iWidthToDraw );
  147.   if ( iWidthToDraw < 0 ) {
  148.     return 1;
  149.   }
  150.   //now do Y
  151.   int iOffsetFromTop;
  152.   int iHeightToDraw;
  153.   _ClipY( Y, iHeight, &iOffsetFromTop, &iHeightToDraw );
  154.   if ( iHeightToDraw < 0 ) {
  155.     return 1;
  156.   }
  157.  
  158.   BYTE * pbDest = pbVGABuffer + ScrnOffs +
  159.         ScrnLogicalByteWidth * ( Y + iOffsetFromTop ) +
  160.         X / 4 + iOffsetFromLeft;
  161.   BYTE * pbDestCounter = pbDest;
  162.  
  163.   outp( SC_INDEX, MAP_MASK );
  164.   int iPlane = X & 3;
  165.   int iAdjust = 0;
  166.   for( int iPlaneCounter = 4; iPlaneCounter != 0; --iPlaneCounter ) {
  167.     pbDestCounter = pbDest + iAdjust;
  168.     pbSource = Bitmap + 2 + ( 4 - iPlaneCounter ) * iWidth * iHeight + iOffsetFromTop * iWidth;
  169.       outp( SC_INDEX + 1, 1 << iPlane );
  170.     for ( int iRowCounter = iHeightToDraw; iRowCounter != 0; --iRowCounter ) {
  171.       memcpy( pbDestCounter, pbSource + iOffsetFromLeft, iWidthToDraw );
  172.       pbSource += iWidth;
  173.       pbDestCounter += ScrnLogicalByteWidth;
  174.     }
  175.     iPlane++;
  176.     if ( iPlane == 4 ) {
  177.       iAdjust = 1;
  178.     }
  179.     iPlane &= 3;
  180.   }
  181.   return 0;
  182. }
  183.